Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
How do I allow async ("click" handler) function to access variables defined by mainline code?

NOTE: Originally titled "How do I allow a button "Click" handler to access variables defined by the code that creates the button?"

I have code that runs in a report web page for a COTS product via the jQuery.ready() function. My code has 2 parts: The "setup" code checks for a bunch of prerequisites and, if all conditions are good, it creates a button & adds a click handler for the button using AddEventListener. The second part is the click handler. In that I need most of the same variables that I defined in the "setup" portion. The quick and dirty way to do it is just copy the code from the "setup" but that seems inefficient and prone to error if I change something in the "setup" part and forget to replicate it in the "click handler".

Is there a way I can pass the variables I define in the "setup" section to an asynchronous event like a click handler?

// the click handler
const delAllSelected = () => {
   // In here I also need the "top_frame", "rpt_doc", "tbl_rptOutput" variables 
   //   defined in the "setup" portion.
   //  ... do stuff ...
}


// setup portion 
jQuery(document).ready(function () {
   var top_frame= top.window.document.querySelector("[name*=reportDialog]");
   var rpt_doc = top?.window?.frames[0]?.frames["List"]?.document;
   var tbl_rptOutput = rpt_doc.querySelector("#ReportOutput");
    ...etc...
   // if all the stars align, create a button and attach click handler
   if (all conditions good) {
       rpt_doc.querySelector("#CheckboxButtons td.btnFrame").insertAdjacentHTML("beforeend",`
       ... html for the "Delete All Selected" button goes here ...
       `);
       tbl_rptOutput.querySelector("#DeleteAllSelected").addEventListener("click",delAllSelected);
    }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda